﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using iuPlace.Core.Models;
using iuPlace.Core.Services;
using iuPlace.Web.Extensions;


namespace iuPlace.Web.Api.Controllers
{
    public class UserController : BaseController
    {
        public JsonResult Login(User user)
        {
            using (var service = this.CurrentSession.ExecuteService<UserService>())
            {
                service.Login(user);
                FormsAuthentication.SetAuthCookie(user.UId.ToString(), false);
                System.Security.Principal.GenericIdentity identity = new System.Security.Principal.GenericIdentity(user.UId.ToString(), "Forms");
                System.Security.Principal.GenericPrincipal principal = new System.Security.Principal.GenericPrincipal(identity, new string[] { });
                this.HttpContext.User = principal;
                return this. JsonResultData(this.CurrentSession);
            }          
        }

        [Authorize]
        public JsonResult Logout()
        {
            using (var service = this.CurrentSession.ExecuteService<UserService>())
            {
                service.Logout();
                FormsAuthentication.SignOut();
                return this.JsonResultData(null);
            }
        }
    }
}
